﻿// Autore: Emanuele Poggi

using System;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using NUnit.Framework;
using RecipesManager.Data;
using TechTalk.SpecFlow;
using White.Core.Factory;
using White.Core.UIItems.WindowItems;
using Button = White.Core.UIItems.Button;

namespace RecipesManager.Specs.Evento
{
    [Binding]
    [Scope(Feature = "ModificaEvento")]
    class ModificaEvento_Steps:EventiBdd
    {
        Window _dialog;
        Button _confirm;
        Button _cancel;
        Button _accept;

        void GetAcceptDialogUi()
        {
            _dialog = MainWindow.ModalWindows()[0];
            _accept = MainWindow.Get<Button>("2");
        }

        void GetConfirmDialogUi()
        {
            _dialog = MainWindow.ModalWindows()[0];
            _confirm = MainWindow.Get<Button>("6");
            _cancel = MainWindow.Get<Button>("7");
        }

        [Then(Regex = @"appare un messaggio")]
        public void AppareMessaggio()
        {
            Assert.That(MainWindow.ModalWindows().Count() == 1);
        }

        [When(Regex = "confermo")]
        public void QuandoConfermo()
        {
            this.GetConfirmDialogUi();
            _confirm.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [When(Regex = "rifiuto")]
        public void QuandoRifiuto()
        {
            this.GetConfirmDialogUi();
            _cancel.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [When(Regex = "accetto")]
        public void QuandoAccetto()
        {
            this.GetAcceptDialogUi();
            _accept.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [BeforeScenario]
        public new void SetUpScenario()
        {
            base.SetUpScenario();
        }

        [AfterScenario]
        public new void TearDownScenario()
        {
            base.TearDownScenario();
        }

        [AfterStep]
        [BeforeStep]
        public void MiniStop()
        {
            MainWindow.WaitWhileBusy();
            MainWindow.ReloadIfCached();
            MainWindow.ReInitialize(InitializeOption.NoCache);
        }

        [Given(Regex = "l'evento \"([a-zA-Z ]+)\" esiste")]
        public void DatoEsisteEvento(string evento)
        {
            Manager.Events.Add(evento);
        }

        [Given(Regex = "seleziono l'evento \"([a-zA-Z ]+)\"")]
        public void DateEventoSelezionato( string evento )
        {
            NavigateToEvents();
            GetEventUi();

            EventsList.Rows[0].Click();

            GetSingleEventUi();
        }

        [Given(Regex = "ho inserito i valori")]
        public void DatoInseritoValori(Table table)
        {
            foreach (var tableRow in table.Rows)
            {
                if (tableRow["Attributi"].Equals("Nome"))
                {
                    EventName.SetValue(tableRow["Valori"]);
                }
                else if (tableRow["Attributi"].Equals("Data"))
                {

                    EventCalendar.Click();

                    Cursor.Position = new Point((int)EventCalendar.Location.X - 495,
                                                (int)EventCalendar.Location.Y + 45);
                    MainWindow.Mouse.Click();

                    Cursor.Position = new Point((int)EventCalendar.Location.X - 452,
                                                (int)EventCalendar.Location.Y + 82);
                    MainWindow.Mouse.Click();

                    Cursor.Position = new Point((int)EventCalendar.Location.X - 338,
                                                (int)EventCalendar.Location.Y + 200);
                    MainWindow.Mouse.Click();

                    Cursor.Position = new Point((int)EventCalendar.Location.X - 365,
                                                (int)EventCalendar.Location.Y + 230);
                    MainWindow.Mouse.Click();
                    MainWindow.Mouse.Click();
                    MainWindow.Mouse.Click();
                    MainWindow.Mouse.Click();

                    Cursor.Position = new Point((int)EventCalendar.Location.X - 450,
                                                (int)EventCalendar.Location.Y + 220);
                    MainWindow.Mouse.Click();
                    MainWindow.Mouse.Click();
                }

                else if (tableRow["Attributi"].Equals("Note"))
                {
                    Note.Click();
                    Cursor.Position = new Point((int)Note.Location.X + 20, (int)Note.Location.Y + 30);
                    MainWindow.Mouse.Click();
                    MainWindow.Enter(tableRow["Valori"]);
                }

                else if (tableRow["Attributi"].Equals("Invitato"))
                {
                    EventsGuests.Enter(tableRow["Valori"]);
                    AddEventGuest.Click();
                }

                else if (tableRow["Attributi"].Equals("Ricetta"))
                {
                    var ricetta = tableRow["Valori"];
                    Menu.Enter(ricetta);
                    AddRecipe.Click();
                }

                else if (tableRow["Attributi"].Equals("Porzioni"))
                {
                    var num = int.Parse(tableRow["Valori"]);
                    Cursor.Position = new Point((int)AddRecipe.Location.X - 35, (int)AddRecipe.Location.Y + 55);
                    //Thread.Sleep(5000);
                    for (var i = 1; i < num; i++)
                        MainWindow.Mouse.Click();
                }

                else
                {
                    Assert.Fail("Table");
                }
            }
        }


        [Then(Regex = "non esiste l'evento \"([a-zA-Z ]+)\"")]
        public void AlloraNonEsisteEvento(string evento)
        {
            var events = Manager.Events.AsQueryable();
            var ev = events.Where(e => e.Name == evento);

            Assert.That(!ev.Any());
        }

        [Then(Regex = "l'evento \"([a-zA-Z ]+)\" è chiuso")]
        public void AlloraEventoChiuso(string evento)
        {
            var events = Manager.Events.AsQueryable();
            var ev = events.Where(e => e.Name == evento && e.HasEnded);

            Assert.That(ev.Any());
        }

        [When(Regex = "salvo")]
        public void QuandoSalvo()
        {
            Assert.That(Save.Enabled);
            Save.Click();
            Manager.ClearCache();
        }

        [When(Regex = "chiudo l'evento")]
        public void QuandoChiudoEvento()
        {
            Assert.That(CloseEvent.Enabled);
            CloseEvent.Click();
            Manager.ClearCache();
        }

        [Then(Regex = "esiste l'evento \"([a-zA-Z ]+)\"")]
        public void AlloraEsisteEvento(string evento)
        {
            var events = Manager.Events.AsQueryable();
            var ev = events.Where(e => e.Name == evento);

            Assert.That(ev.Any());
        }

        [Then(Regex = "esiste l'evento chiuso \"([a-zA-Z ]+)\"")]
        public void AlloraEsisteEventoChiuso(string evento)
        {
            var events = Manager.Events.AsQueryable();
            var ev = events.Where(e => e.Name == evento && e.HasEnded);

            Assert.That(ev.Any());
        }

        [Then(Regex = "esiste l'evento con i valori")]
        public void AlloraEsisteEventoConData(Table table)
        {
            var events = Manager.Events.AsQueryable();

            foreach (var tableRow in table.Rows)
            {
                if (tableRow["Attributi"].Equals("Nome"))
                {
                    var evento = tableRow["Valori"];
                    events = events.Where(e => e.Name.Equals(evento));
                }
                else if (tableRow["Attributi"].Equals("Data"))
                {
                    var data = DateTime.ParseExact(tableRow["Valori"], "MM/dd/yyyy",
                                                        CultureInfo.InvariantCulture, DateTimeStyles.None);
                    events = events.Where(e => e.Date.Equals(data));
                }

                else if (tableRow["Attributi"].Equals("Invitato"))
                {
                    var guest = tableRow["Valori"];
                    events = events.Where(e => e.Guests.Any(g => g.Name.Equals(guest)));
                }

                else if (tableRow["Attributi"].Equals("Ricetta"))
                {
                    var ricetta = tableRow["Valori"];
                    events = events.Where(e => e.Recipes.Any(r => r.Name.Equals(ricetta)));
                }

                else if (tableRow["Attributi"].Equals("Porzioni"))
                {
                    var num = int.Parse(tableRow["Valori"]);

                    events = events.Where(e => e.Recipes.Any(r => r.Servings == num));
                }
                else if (tableRow["Attributi"].Equals("Note"))
                {
                    var note = tableRow["Valori"];
                    events = events.Where(e => e.Notes!="");
                }

                else
                {
                    Assert.Fail("Table");
                }
            }

            Assert.That(events.Any());
        }
    }
}
